let has = Object.prototype.hasOwnProperty;

let maxSafeInteger = Math.pow(2, 32) - 1;

let toInteger = function (value) {
  const number = Number(value);
  if (isNaN(number)) {
    return 0;
  }
  if (number === 0 || !isFinite(number)) {
    return number;
  }
  return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
};

let toLength = function (value) {
  let len = toInteger(value);
  return Math.min(Math.max(len, 0), maxSafeInteger);
};

let push = Array.prototype.push;

Array.prototype.flat = function (deep) {
  let O = Object(this);
  let sourceLen = toLength(O.length);
  let depthNum = 1;
  if (deep !== undefined) {
    depthNum = toLength(deep);
  }
  if (depthNum <= 0) {
    return O;
  }
  let arr = [];

  let val;
  for (let i = 0; i < sourceLen; i++) {
    if (has.call(O, i)) {
      val = O[i];
      if (Array.isArray(val)) {
        push.apply(arr, val.flat(depthNum - 1));
      } else {
        arr.push(val);
      }
    } else {
      arr.push(undefined);
    }
  }

  return arr;
};

let array = [1, 3, [4, 5], [6, [7, 8, [9, , 10]]], [, ,], [undefined, null, NaN]];

console.log(array.flat(2));
